#!/bin/bash
set -u
APP="/Applications/RustDesk.app"
PREF="$HOME/Library/Preferences/com.carriez.RustDesk"
HERE="$(cd "$(dirname "$0")/../Resources" && pwd)"
CFG="$HERE/RustDesk2.toml"

osascript -e 'tell application "RustDesk" to quit' >/dev/null 2>&1 || true
sleep 1

if [ ! -d "$APP" ]; then
  osascript -e 'display dialog "RustDesk n’est pas installé dans le dossier Applications.\n\nGlissez d’abord RustDesk dans Applications, puis relancez Assistance RustDesk." buttons {"OK"} default button "OK" with icon caution'
  exit 1
fi

mkdir -p "$PREF"
cp "$CFG" "$PREF/RustDesk2.toml"
chmod 600 "$PREF/RustDesk2.toml" 2>/dev/null || true

open -a "$APP"
sleep 2
osascript -e 'display dialog "RustDesk est configuré.\n\nDonnez simplement l’ID affiché dans RustDesk à votre technicien." buttons {"OK"} default button "OK" with title "Assistance RustDesk"'
